import { HashMap } from "../../api/struct/HashMap";
import { CDEquipmentSuit, E_CDEquipmentSuit } from "../excel/CDEquipmentSuit";
import { CEEquipmentRune } from "../excel_extend/CEEquipmentRune";
import { CESuit } from "../excel_extend/CESuit";
import { Player } from "../struct/Player";
import { SDEquip } from "./SDEquip";


/**
 * 数据-套装
 */
export abstract class SDSuits {


    /**
     * 获取已穿套装（套装id，[装备id（可能是符文代替的）]）
     * @param player 
     * @param suits 
     * @param runeActive 
     * @returns 
     */
    public static getWearSuits(player: Player, suits = new HashMap<number, number[]>(), runeActive = true): HashMap<number, number[]> {
        //清空数据
        suits.clear();

        const em = player.equipManager;
        const wearIDs = em.getWearValues();

        //统计
        for (let i = wearIDs.length - 1; i >= 0; i--) {
            const equip = em.getEquip(wearIDs[i]);
            if (!equip) {
                continue;
            }
            const equipConfig = SDEquip.getEquipConfig(equip);
            if (!equipConfig || equipConfig.suit === 0) {
                continue;
            }
            let list = suits.get(equipConfig.suit);
            if (!list) {
                list = [];
                suits.put(equipConfig.suit, list);
            }
            list.push(equip.equipId);
        }

        // 符文的替代
        if (runeActive && suits.size() > 0) {
            const ks = suits.getKeys();
            const vs = suits.getValues();
            const suitRuneMap = CEEquipmentRune.suitRuneMap;
            for (let i = ks.length - 1; i >= 0; i--) {
                const suitID = ks[i];
                const suitEquipIDList = CESuit.suitMap.get(suitID);
                if (!suitEquipIDList) {
                    continue;   //无套装配置
                }

                const haveList = vs[i];
                if (haveList.length >= suitEquipIDList.length) {
                    continue;   //都有了
                }

                for (let j = 0, suitIDLen = suitEquipIDList.length; j < suitIDLen; j++) {
                    const equipID = suitEquipIDList[j];
                    if (haveList.indexOf(equipID) >= 0) {
                        continue;   //已穿
                    }

                    const runeIDList = suitRuneMap.get(equipID);
                    if (!runeIDList) {
                        continue;   //可替代指定装备的符文不存在
                    }

                    //依次看看符文是否已拥有
                    for (let k = 0, runeListLen = runeIDList.length; k < runeListLen; k++) {
                        if (0 < player.getItemNum(runeIDList[k])) {
                            haveList.push(equipID); //用符文代替装备
                            break;
                        }
                    }
                }
            }
        }

        return suits;
    }


    /**
    * 套装技能是否生效
    * @param config 
    * @param wearCount 
    */
    public static isSuitSkillOpen(suit: CDEquipmentSuit, skillIndex: number, wearCount: number): boolean {
        if (!suit || wearCount <= 0) {
            return false;
        }
        if (suit.getAttr(E_CDEquipmentSuit.skill_1 + skillIndex) === 0) {
            return false;
        }
        if (wearCount - 2 < skillIndex) {  //2->0, 3->0,1 4->0,1,2
            return false;
        }
        return true;
    }



}
